home *** CD-ROM | disk | FTP | other *** search
- Path: news.uoregon.edu!xmission!news
- From: tknarr@xmission.com ( Todd Knarr )
- Newsgroups: comp.lang.c++
- Subject: Re: Date class
- Date: 4 Feb 1996 18:03:04 GMT
- Organization: Chaos Central
- Message-ID: <4f2sco$bct@news.xmission.com>
- References: <4f09k2$nnu@george.rutgers.edu>
- Reply-To: tknarr@xmission.com ( Todd Knarr )
- NNTP-Posting-Host: slc68.xmission.com
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <4f09k2$nnu@george.rutgers.edu>, aminabdu@george.rutgers.edu (Amin Abdulghani) writes:
- >I am trying to build a small prototype which involves lot of manipulation with
- >dates i.e. incrementing/decrementing dates by a week, month and so on. Is
- >anyone aware of any available classes which already do such manipulations. Any
- >pointers would be helpful.
-
- I don't know if this is exactly what you are looking for, but
- this is a class I came up with for handling dates. The arithmetic
- deals with adding/subtracting days to/from a date, so you'd either
- have to have a table of days per month or convert to YMD, add one
- to M and convert back to a Date. The << and >> operators aren't
- included, mostly because thanks to the code this has to fit into I
- can't use << and >>.
-
- #ifndef CLASS_DATE_H
- #define CLASS_DATE_H
-
- //
- // Classname : Date
- //
- // Author : Todd Knarr
- //
- // Description :
- // Provides a Date class which represents dates as Julian day numbers
- // ( days since 1 Jan 4713 BC ).
- // This class can handle all dates from 1 Jan 4713BC to 31 Dec 9999AD.
- //
- // Note: Years AD are positive, years BC are negative. There is
- // no year 0AD, it goes from 1BC to 1AD. A year of 0 will be treated
- // as 1BC. No attempt is made to validate ranges. Physical errors
- // will not result from insane day-month combinations like the 87th
- // day of the 45th month, but the results will obviously not make
- // much sense.
- //
- // Date conversion routines by Eric Bergman-Terrell,
- // Computer Language Dec 1990.
- //
-
- #include <stdlib.h>
- #include <time.h>
-
- class Date
- {
-
- private:
-
- long lJulianDay;
-
- //
- // Function : YmdToJd
- //
- // Parameters : int year, month, day
- //
- // Return values : long julian day
- //
- // Description : converts year/month/day to julian day
- //
- static long YmdToJd( const int iYear, const int iMonth, const int iDay );
-
- //
- // Function : JdToYmd
- //
- // Parameters : long julian day, pointers to int year, month, day
- //
- // Return values : none
- //
- // Description : converts julian day to year/month/day
- //
- static void JdToYmd( const long lJD, int *piYear, int *piMonth,
- int *piDay );
-
- //
- // Function : Addition, Subtraction helper functions
- //
- // Parameters :
- //
- // Return values :
- //
- // Description : Adds days to current Date, subtracts days from
- // current Date, takes difference of two Dates in days.
- //
- Date& Add( const long iDays )
- {
- lJulianDay += iDays;
- return *this;
- }
- Date& Subtract( const long iDays )
- {
- lJulianDay -= iDays;
- return *this;
- }
- long Subtract( const Date Other ) const
- {
- return lJulianDay - Other.lJulianDay;
- }
-
- public:
-
- //
- // Function : IsLeapYear
- //
- // Parameters : int year
- //
- // Return values : int
- //
- // Description : returns 1 if the given year is a leap year,
- // 0 otherwise
- //
- static int IsLeapYear( const int iYear )
- {
- long jd1, jd2;
- jd1 = YmdToJd( iYear, 2, 29 );
- jd2 = YmdToJd( iYear, 3, 1 );
- return (int) ( jd2 - jd1 );
- }
-
- //
- // Function : default constructor
- //
- // Parameters : none
- //
- // Return values : none
- //
- // Description : constructs a new object initialized to 1 Jan 4713BC
- //
- Date() { lJulianDay = 0L; }
-
- //
- // Function : time_t constructor
- //
- // Parameters : none
- //
- // Return values : none
- //
- // Description : constructs an object initialized to the date
- // represented by a system time value.
- //
- Date( const time_t tSysTime )
- {
- struct tm *ptm;
-
- ptm = localtime( &tSysTime );
- lJulianDay = YmdToJd( ptm->tm_year, ptm->tm_mon, ptm->tm_mday );
- }
-
- //
- // Function : char* ( string ) constructor
- //
- // Parameters :
- //
- // Return values :
- //
- // Description : constructs an object from a string.
- // The string is formatted as the ASCII representation
- // of the long Julian day number.
- //
- Date( const char *String )
- {
- lJulianDay = atol( String );
- }
-
- //
- // Function : year/month/day constructor
- //
- // Parameters : int year, month, day
- //
- // Return values : none
- //
- // Description : constructs an object initialized to
- // the date given by the arguments
- //
- Date( const int iDay, const int iMonth, const int iYear )
- {
- lJulianDay = YmdToJd( iYear, iMonth, iDay );
- }
-
- //
- // Function : Year, Month, Day, DayOfYear, DayOfWeek, IsLeapYear, YMD
- //
- // Parameters : none
- //
- // Return values : int year, month or day
- //
- // Description : Returns the year, month or day corresponding
- // to the date, the day of the year and the day of the
- // week matching what localtime() gives ( 0 = Sunday,
- // 6 = Saturday ). YMD() produces the year, month and day
- // of the object in one operation.
- //
- int Year( void ) const
- {
- int y, m, d;
-
- JdToYmd( lJulianDay, &y, &m, &d );
- return y;
- }
- int Month( void ) const
- {
- int y, m, d;
-
- JdToYmd( lJulianDay, &y, &m, &d );
- return m;
- }
- int Day( void ) const
- {
- int y, m, d;
-
- JdToYmd( lJulianDay, &y, &m, &d );
- return d;
- }
- int DayOfYear( void ) const;
- int DayOfWeek( void ) const
- {
- return ( ( ( (int) ( lJulianDay % 7L ) ) + 1 ) % 7 );
- }
- int IsLeapYear( void ) const
- {
- int y, m, d;
- JdToYmd( lJulianDay, &y, &m, &d );
- return IsLeapYear( y );
- }
- void YMD( int *pY, int *pM, int *pD )
- {
- JdToYmd( lJulianDay, pY, pM, pD );
- return;
- }
-
- //
- // Function : Addition operators
- //
- // Parameters :
- //
- // Return values :
- //
- // Description : Adding an integral type to a Date adds
- // the number of days in the integral type to the Date.
- // Subtracting an integral type from a date subtracts the
- // number of days in the integral type from the Date.
- // Subtracting two Dates gives the number of days between
- // them as a long. Adding two Dates is not defined.
- //
- friend Date operator+( const Date& Left, const long Right )
- {
- Date Temp = Left;
- Temp.Add( Right );
- return Temp;
- }
- friend Date operator+( const long Left, const Date& Right )
- {
- Date Temp = Right;
- Temp.Add( Left );
- return Temp;
- }
- Date& operator+=( const long Right )
- {
- Add( Right );
- return *this;
- }
- friend Date operator-( const Date& Left, const long Right )
- {
- Date Temp = Left;
- Temp.Subtract( Right );
- return Temp;
- }
- friend Date operator-( const long Left, const Date& Right )
- {
- Date Temp = Right;
- Temp.Subtract( Left );
- return Temp;
- }
- Date& operator-=( const long Right )
- {
- Subtract( Right );
- return *this;
- }
- friend Date operator+( const Date& Left, const unsigned long Right )
- {
- Date Temp = Left;
- Temp.Add( Right );
- return Temp;
- }
- friend Date operator+( const unsigned long Left, const Date& Right )
- {
- Date Temp = Right;
- Temp.Add( Left );
- return Temp;
- }
- Date& operator+=( const unsigned long Right )
- {
- Add( (long) Right );
- return *this;
- }
- friend Date operator-( const Date& Left, const unsigned long Right )
- {
- Date Temp = Left;
- Temp.Subtract( (long) Right );
- return Temp;
- }
- friend Date operator-( const unsigned long Left, const Date& Right )
- {
- Date Temp = Right;
- Temp.Subtract( (long) Left );
- return Temp;
- }
- Date& operator-=( const unsigned long Right )
- {
- Subtract( (long) Right );
- return *this;
- }
- long operator-( const Date& Right )
- {
- return lJulianDay - Right.lJulianDay;
- }
-
- //
- // Function : ++ and -- operators, prefix and postfix forms
- //
- // Parameters :
- //
- // Return values :
- //
- // Description :
- // Increment or decrement a Date by 1 day. The standard
- // semantics for prefix/postfix apply.
- //
- Date& operator++()
- {
- lJulianDay++;
- return *this;
- }
- Date operator++( int )
- {
- Date Temp = *this;
- lJulianDay++;
- return Temp;
- }
- Date& operator--()
- {
- lJulianDay++;
- return *this;
- }
- Date operator--( int )
- {
- Date Temp = *this;
- lJulianDay++;
- return Temp;
- }
-
- //
- // Function : ToString
- //
- // Parameters : pointer to string buffer to fill in
- //
- // Return values : none
- //
- // Description : Formats the Date into an ASCII representation.
- // This is the ASCII form of the long Julian day number.
- // The string is a fixed-length 12-character string, including
- // the NUL terminator;
- //
- void ToString( char *szBuffer ) const;
-
- //
- // Function : comparison operators
- //
- // Parameters :
- //
- // Return values :
- //
- // Description :
- //
- int operator==( const Date& Right ) const
- {
- return lJulianDay == Right.lJulianDay;
- }
- int operator!=( const Date& Right ) const
- {
- return lJulianDay != Right.lJulianDay;
- }
- int operator<( const Date& Right ) const
- {
- return lJulianDay < Right.lJulianDay;
- }
- int operator<=( const Date& Right ) const
- {
- return lJulianDay <= Right.lJulianDay;
- }
- int operator>( const Date& Right ) const
- {
- return lJulianDay > Right.lJulianDay;
- }
- int operator>=( const Date& Right ) const
- {
- return lJulianDay >= Right.lJulianDay;
- }
-
- //
- // Function : ToSysTime
- //
- // Parameters : none
- //
- // Return values : converted result
- //
- // Description : Converts the date to a time_t value
- // representing midnight of that date.
- //
- time_t ToSysTime( void ) const;
-
- };
-
- #endif
-
- --
- Todd Knarr : tknarr@xmission.com | finger for PGP public key
- | Member, USENET Cabal
-
- Seriously, I don't want to die just yet. I don't care how
- good-looking they are, I! don't! want! to! die!"
- -- Megazone ( UF1 )
-
-